package com.pulisher.controller;

import com.pulisher.bean.ExamCourseScoreBean;
import com.pulisher.service.ExamCourseService;
import com.pulisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ExamCourseController {
    @Autowired
    private ExamCourseService examCourseService;

    @RequestMapping("/score")
    public String getCourseScore(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<ExamCourseScoreBean> examCourseScoreBeans = examCourseService.getCourseScore(date);

        StringBuilder json = new StringBuilder("{\"status\": 0,\"data\": {\"categories\": [");

        for (int i = 0; i < examCourseScoreBeans.size(); i++) {
            ExamCourseScoreBean examCourseScoreBean = examCourseScoreBeans.get(i);
            json.append("\""+examCourseScoreBean.getCourse_name().replaceAll("\\\\","/")+"\"");
            if (i < examCourseScoreBeans.size() - 1){
                json.append(",");
            }
        }
        json.append("],\"series\": [{\"name\": \"课程名称\",\"data\": [");
        for (int i = 0; i < examCourseScoreBeans.size(); i++) {
            ExamCourseScoreBean examCourseScoreBean = examCourseScoreBeans.get(i);
            json.append(examCourseScoreBean.getScore());
            if (i < examCourseScoreBeans.size() - 1){
                json.append(",");
            }
        }
        json.append("]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}");
        return json.toString();
    }
}